<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class news extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("mnews");
        $this->load->model("mnewscate");
        $this->load->model("mresource");
		
		$this->resCode = $this->mnews->resCode;
        $this->url_index = admin_url() . "news";
        $this->folder_upload = "dataupload/news/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
        $data = array();
        $data['datas'] = $this->mnews->gets();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $this->theme_data["main"] = "news/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($manID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['news'] = $this->mnews->get($manID, 'NEWS_ID');
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
			if($upload = $this->upload(@$data['news']->NEWS_THUMB)){
				$post['news']['NEWS_THUMB'] = $this->folder_upload.$upload['upload_data']['file_name'];
			}
            if ($manID > 0) {
                $this->mnews->update($manID, 'NEWS_ID', $post['news']);
            }
			else{
				$manID = $this->mnews->insert($post['news']);
			}
            redirect($this->url_edit . $manID);
        }
		$infocate = $this->mnewscate->gets();
		$data['infocate'] = array();
		foreach($infocate as $r){
			$data['infocate'][$r->NEWSCATE_ID] =$r->NEWSCATE_TITLE;
		}
		$data['images'] = array();
		if($manID >0)
			$data['images'] = $this->mnews->getImageDetail($manID);
        $this->theme_data["main"] = "news/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($manID = 0){
		if($manID > 0){
			$this->mnews->delete($manID, 'NEWS_ID');
		}
	}
	
	function upload($old_name = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		
		//$config['max_size']	= '10000';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
		
	}
	
	function do_upload_products_image() {
        
        $infoID = $this->input->post('param1');
		 if(!$infoID || $infoID <0 || $infoID ==''){
			return false;
		 }
		 $info = $this->mnews->get($infoID,'NEWS_ID');
		 if(!$info){
			 return false;
		 }
			
			$config['upload_path'] = './'.$this->folder_upload.'images/';
			$config['allowed_types'] = 'gif|jpg|png|';
			$config['file_name'] = $info->NEWS_CODE.'-'.$info->NEWS_ID;
			$this->load->library('upload', $config);
			if(!is_dir($this->folder_upload))
				mkdir($this->folder_upload,0777);
			if(!is_dir($config['upload_path']))
				mkdir($config['upload_path'],0777);
			if (!$this->upload->do_upload('Filedata'))
			{
				echo $this->upload->display_errors('<p>', '</p>');

				return false;
			}
			else
			{
				$newFile = $this->upload->data();
			}
				 $array=array(
				 	"NEWS_ID"=>$infoID,
					"NEWS_IP"=>$this->input->ip_address(),
					"NEWS_TIME_CREATE"=>time(),
				 	"NEWS_PATCH"=>$this->folder_upload.'images/' . $newFile['file_name']
				 );
				  $this->db->insert("news_images",$array);
           
               		 echo json_encode('<p class="success-message-box"><strong>Success!</strong> The file:' . $newFile['file_name'] . ' was uploaded.</p>');
                /* end write to database */
    }
	
	function deleteimage(){
		
	}
}
